package main

func main() {
	println(peakIndexInMountainArray([]int{3, 4, 5, 1}))
}
func peakIndexInMountainArray(arr []int) int {
	return binSearch(arr, 0, len(arr)-1)
}

/**
 *	binSearch
 *	@Description: 二分查找
 *	@param arr []int
 *	@param left int
 *	@param right int
 *	@return int
 */
func binSearch(arr []int, left, right int) int {
	if right-left == 1 {
		if arr[right] > arr[left] {
			return right
		}
		return left
	}

	mid := (left + right) >> 1

	if arr[mid] < arr[mid+1] {
		return binSearch(arr, mid, right)
	} else if arr[mid] < arr[mid-1] {
		return binSearch(arr, left, mid)
	} else {
		return mid
	}
}
